package org.example.warehouse.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import org.example.warehouse.domain.OrderDetail;
import org.example.warehouse.service.IOrderDetailService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单详情Controller
 *
 * @author 小孟微信：xiaomengcode
 * @date 2024-04-12
 */
@Controller
@RequestMapping("/warehouse/orderDetail")
public class OrderDetailController extends BaseController
{
    private String prefix = "warehouse/orderDetail";

    @Autowired
    private IOrderDetailService orderDetailService;

    @GetMapping()
    public String orderDetail(String orderId,String contractType,ModelMap mmp)
    {
        mmp.put("orderId",orderId);
        mmp.put("contractType",contractType);
        return prefix + "/orderDetail";
    }

    /**
     * 采购
     */
    @GetMapping("/mallOrderDetail")
    public String mallOrderDetail(String orderDetailId,String orderId,String contractType,String contractId,String roleFlag,ModelMap mmp)
    {
        mmp.put("orderDetailId",orderDetailId);
        mmp.put("orderId",orderId);
        mmp.put("contractId",contractId);
        mmp.put("contractType",contractType);
        mmp.put("roleFlag",roleFlag);

        return "warehouse/mallStore/mallStore";
    }

    /**
     * 销货
     */
    @GetMapping("/sellOrderDetail")
    public String sellOrderDetail(String orderDetailId,String orderId,String contractType,String contractId,String roleFlag,ModelMap mmp)
    {
        mmp.put("orderId",orderId);
        mmp.put("orderDetailId",orderDetailId);
        mmp.put("contractId",contractId);
        mmp.put("contractType",contractType);
        mmp.put("roleFlag",roleFlag);

        return "warehouse/sellStore/sellStore";
    }

    /**
     * 查询订单详情列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OrderDetail orderDetail)
    {
        startPage();
        List<OrderDetail> list = orderDetailService.selectOrderDetailList(orderDetail);
        return getDataTable(list);
    }

    /**
     * 导出订单详情列表
     */
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OrderDetail orderDetail)
    {
        List<OrderDetail> list = orderDetailService.selectOrderDetailList(orderDetail);
        ExcelUtil<OrderDetail> util = new ExcelUtil<OrderDetail>(OrderDetail.class);
        return util.exportExcel(list, "订单详情数据");
    }

    /**
     * 新增订单详情
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存订单详情
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(OrderDetail orderDetail)
    {
        return toAjax(orderDetailService.insertOrderDetail(orderDetail));
    }

    /**
     * 修改订单详情
     */
    @GetMapping("/edit/{orderDetailId}")
    public String edit(@PathVariable("orderDetailId") Long orderDetailId, ModelMap mmap)
    {
        OrderDetail orderDetail = orderDetailService.selectOrderDetailByOrderDetailId(orderDetailId);
        mmap.put("orderDetail", orderDetail);
        return prefix + "/edit";
    }

    /**
     * 修改订单详情
     */
    @GetMapping("/edit/{orderDetailId}/{contractType}")
    public String edit(@PathVariable("orderDetailId") Long orderDetailId,@PathVariable("contractType") String contractType ,ModelMap mmap)
    {
        OrderDetail orderDetail = orderDetailService.selectOrderDetailByOrderDetailId(orderDetailId);
        orderDetail.setContractType(contractType);
        mmap.put("orderDetail", orderDetail);
        return prefix + "/edit";
    }

    /**
     * 修改保存订单详情
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OrderDetail orderDetail)
    {
        return toAjax(orderDetailService.updateOrderDetail(orderDetail));
    }

    /**
     * 删除订单详情
     */
    @Log(title = "订单详情", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(orderDetailService.deleteOrderDetailByOrderDetailIds(ids));
    }
}
